package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingLevelServiceImpl implements NursingLevelService {
    private final NursingLevelMapper nursingLevelMapper;
    @Override
    public List<NursingLevelVo> findAll() {
        List<NursingLevel> nursingLevels = nursingLevelMapper.listAll();
        return BeanUtil.copyToList(nursingLevels, NursingLevelVo.class);
    }

    @Override
    public void addNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
        int insert = nursingLevelMapper.insert(nursingLevel);
        if (insert <=0){
            throw new RuntimeException("新增失败");
        }
    }

    @Override
    public PageResponse<NursingLevelVo> selectPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //分页插件创建分页
        PageHelper.startPage(pageNum,pageSize);
        Page<List<NursingLevel>> listByPage = nursingLevelMapper.listByPage(name, status,pageNum,pageSize);
        return PageResponse.of(listByPage, NursingLevelVo.class);
    }

    @Override
    public NursingLevelVo selectById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        return BeanUtil.copyProperties(nursingLevel, NursingLevelVo.class);
    }

    @Override
    public void updateNursingLevel(NursingLevelDto nursingLevelDto) {
        nursingLevelMapper.update(BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class));
    }

    @Override
    public void startOrStop(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }

    @Override
    public void deleteById(Long id) {
        nursingLevelMapper.delete(id);
    }
}
